package com.zyz.M_File和IO流.IO流.节点流.Reader和Writer;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author: vick
 * @Date: 2025/4/8
 * @Description:
 * @version: 1.0
 */
public class Demo6 {
    public static void main(String[] args) {

        FileWriter fileWriter = null;
        try {
            //1. 创建File的对象
            File file = new File("fw.txt");
            //2. 创建FileWriter的对象，将File对象作为参数传递到FileWriter的构造器中
            //如果输出的文件已存在，则对现有的文件进行覆盖
            fileWriter = new FileWriter(file);
            // fileWriter = new FileWriter(file, false);
            //如果输出的文件已存在，则在现有的文件进行追加写入数据
            // fileWriter = new FileWriter(file, true);
            fileWriter.write("I love you");
            fileWriter.write(" but  you love him");
            fileWriter.write(" say");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (fileWriter != null) {
                    fileWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
